# !/usr/bin/env python
#
# Copyright 2008 CPedia.com.
#
# New portions Copyright 2009, 2010 Rob Stenzinger 
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = 'Ping Chen, Rob Stenzinger'

import wsgiref.handlers

from google.appengine.ext import webapp

import rpc
import blog
import logging
import util
import os
import publish

from google.appengine.ext.webapp import template
template.register_template_library('filter.replace')
template.register_template_library('filter.gravatar')


def main():
    config = util.get_config()
    application = webapp.WSGIApplication(
                                       [
                                        ('/assets/(.*)', blog.ThemeAssetHandler),  #for theme assets
                                        ('/favicon.ico', blog.ThemeAssetHandlerFavIco),  #for theme assets
                                        ('/favicon.png', blog.ThemeAssetHandlerFavPNG),  #for theme assets
                                        
                                        ('/create/(blog|post|page|comic|audio|video)/*$', blog.AddBlog),

                                        #todo: finish/re-add oembed support
                                        #('/oembed/*', blog.OEmbed),

                                        ('/addBlogReaction', blog.AddBlogReaction),
                                        ('/edit/(blog|page)/(.*)/*$', blog.EditBlog),
                                        ('/delete/(blog|page)/(.*)/*$', blog.DeleteBlog),
                                        ('/edit/comment/(.*)/*$', blog.EditBlogReaction),
                                        ('/delete/comment/(.*)/*$', blog.DeleteBlogReaction),

                                        ('/rpc/uploadImage', rpc.UploadImage),
                                        ('/rpc/uploadFlash', rpc.UploadFlash),
                                        #('/rpc/img/*$', rpc.Image),
                                        ('/rpc/flash/(.*)', rpc.Flash),
                                        ('/rpc/img/(.*)', rpc.ImageREST),
                                        ('/rpc/image/(.*)', rpc.ImageREST),
                                        ('/rpc/imglocal/(.*)', rpc.ImageLocal),
                                        ('/rpc/episode/(.*)', rpc.ImageEpisode),
                                        ('/rpc/episodethumb/(.*)', rpc.ImageEpisodeThumb),                                        
                                        
                                        ('/admin', blog.Admin),
                                        ('/admin/', blog.Admin),
                                        ('/admin/tag/resetentrycount', blog.AdminTagResetCount),
                                        ('/admin/book/resetentrycount', blog.AdminBookResetCount),
                                        ('/admin/chapter/resetentrycount', blog.AdminChapterResetCount),
                                        ('/admin/list/', blog.AdminList),                                  
                                        ('/admin/list/(.*)', blog.AdminListFilter),
                                                                          
                                        
                                        ('/*$', blog.MainPage),
                                        ('/page/(\d*)/*$', blog.BlogPageHandle),
                                        ('/type/(\d*)/*$', blog.BlogFilterHandle),
                                        ('/type/.*', blog.BlogFilterHandle),
                                        ('/filter/(\d*)/.*', blog.BlogFilterHandle),
                                        ('/filter/.*', blog.BlogFilterHandle),

                                        ('/403.html', blog.UnauthorizedHandler),
                                        ('/404.html', blog.NotFoundHandler),
                                        ('/(19|20\d\d)[- /.](0[1-9]|1[012])', blog.ArticleFullDateHandler),
                                        ('/(19|20\d\d)[- /.](0[1-9]|1[012])[- /.]', blog.ArticleFullDateHandler),
                                        ('/(19|20\d\d)[- /.](0[1-9]|1[012])[- /.]', blog.ArticleFullDateHandler),
                                        ('/(19|20\d\d)[- /.](0[1-9]|1[012])[- /.]/(.*)', blog.ArticleFullDateHandler),
                                        ('/(19|20\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])/(.*)', blog.ArticleFullDateHandler),
                                        ('/(19|20\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])/.*', blog.ArticleFullDateHandler),
                                        ('/(19|20\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])', blog.ArticleFullDateHandler),
                                        ('/([12]\d\d\d)/(\d|[01]\d)/([-\w]+)/*$', blog.ArticleHandler),
                                        ('/([12]\d\d\d)/(\d|[01]\d)/([-\w]+)/.*', blog.ArticleHandler),
                                        ('/tag/(\d*)/.*', blog.BlogFilterHandle),
                                        ('/tag/.*', blog.BlogFilterHandle),

                                        ('/atom/*$', blog.FeedHandler),
                                        ('/feed/*$', blog.FeedHandler),

                                        ('/sitemap/*$', blog.SiteMapHandler),  #for live.com SEO

                                        ('/login/*$', blog.Login),
                                        ('/logout/*$', blog.Logout),

                                        ('/archive/(.*)/*$', blog.ArchiveHandler),
                                        
                                        ('/([-\w]+)/*$', blog.PageHandler),                                        
                                        ('/([-\w]+)/.*', blog.PageHandler),
                                       ],
                                       debug=config["debug"])
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()
